﻿
#region Usings
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Xceed.Wpf;
using wpfToolkit = Xceed.Wpf.Toolkit;
using FasterWPF;
#endregion

namespace FasterWPFExtras
{
    public static partial class CommonExtExtras
    {
        /// <summary>
        /// Get the Text from a Fixed Length Masked TextBox (including both values and literals), e.g. (999) 999-9999
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public static string GetTextFromFixedLengthMaskedTextBox<T>(this Border source, int row, int column)
            where T : FrameworkElement
        {
            return source.GetChildFromComposite<wpfToolkit.MaskedTextBox, T>(row, column).Text.ToString();
        }

        /// <summary>
        /// Get the Value from a Numeric TextBox as a Nullable Decimal
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public static decimal? GetValueFromNumericTextBox<T>(this Border source, int row, int column)
            where T : FrameworkElement
        {
            return (source.GetChildFromComposite<wpfToolkit.DecimalUpDown, T>(row, column).Value as decimal?);
        }

        /// <summary>
        /// Add Numeric TextBox to Composite chain. Format string options include: C-Currency, F-Fixed Point, G-General, N-Number, P-Percent
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="value"></param>
        /// <param name="max"></param>
        /// <param name="min"></param>
        /// <param name="increment"></param>
        /// <param name="formatString"></param>
        /// <param name="selectAllOnGotFocus"></param>
        /// <returns></returns>
        public static InnerChain<T> AddNumericTextBox<T>(this InnerChain<T> chain, int row, int col, double width, double height, decimal value, decimal max, decimal min, decimal increment, string formatString, bool selectAllOnGotFocus)
    where T : FrameworkElement, new()
        {
            // C	 Currency
            // F	 Fixed Point
            // G	 General
            // N	 Number
            // P	 Percent

            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "Maximum", max);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "Minimum", min);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "Width", width);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "Height", height);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "Increment", increment);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "SelectAllOnGotFocus", selectAllOnGotFocus);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "FormatString", formatString);
            (chain.Item1 as T).Set<wpfToolkit.DecimalUpDown, T>(row, col, "Value", value);

            chain.Item3.Add(((container) =>
            {
                return container.AddAnything<wpfToolkit.DecimalUpDown, T>(chain.Item2, row, col);
            }));
            return chain;
        }


        /// <summary>
        /// Add Fixed Length TextBox to Composite chain. Ideal for things like Phone Number or Social Security Number. For currency, see AddNumericTextBox
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="chain"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="newText"></param>
        /// <param name="mask"></param>
        /// <param name="hidePromptOnLeave"></param>
        /// <param name="selectAllOnGotFocus"></param>
        /// <returns></returns>
        public static InnerChain<T> AddFixedLengthMaskedTextBox<T>(this InnerChain<T> chain, int row, int col, string newText, string mask, bool hidePromptOnLeave, bool selectAllOnGotFocus)
            where T : FrameworkElement, new()
        {
            (chain.Item1 as T).Set<wpfToolkit.MaskedTextBox, T>(row, col, "Mask", mask);
            (chain.Item1 as T).Set<wpfToolkit.MaskedTextBox, T>(row, col, "Text", newText);
            (chain.Item1 as T).Set<wpfToolkit.MaskedTextBox, T>(row, col, "HidePromptOnLeave", hidePromptOnLeave);

            if (selectAllOnGotFocus)
            {
                (chain.Item1 as T).Set<wpfToolkit.MaskedTextBox, T>(row, col, "AutoSelectBehavior", wpfToolkit.AutoSelectBehavior.OnFocus);
            }
            else
            {
                (chain.Item1 as T).Set<wpfToolkit.MaskedTextBox, T>(row, col, "AutoSelectBehavior", wpfToolkit.AutoSelectBehavior.Never);
            }

            chain.Item3.Add(((container) =>
            {
                return container.AddAnything<wpfToolkit.MaskedTextBox, T>(chain.Item2, row, col);
            }));
            return chain;
        }

    }//end of class
}//end of namespace
